const cheklogin = getApp().globalData.login
const url = getApp().globalData.Host_url;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    login: true,
    userinfo: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {


  },

  // 获取用户信息
  getUserInfo: function (res) {
    var that = this;
    wx.getUserProfile({
      desc: '用于完善会员资料',
      success: (res) => {
        var userinfo = res.userInfo
        wx.login({
          success(res) {
            var data = {
              "username": userinfo['nickName'],
              "jinsi": userinfo['gender'],
              "avatarUrl": userinfo['avatarUrl'],
              "code": res.code
            };
            if (res.code) {
              //发起网络请求
              wx.request({
                url: url + 'Login/login',
                data: {
                  data: data
                },
                method: "POST",
                dataType: "json",
                success: (res) => {
                  if (res.data.code === 200) {
                    wx.setStorageSync('userinfo', res.data.data)
                    that.setData({
                      login: true,
                      userinfo:res.data.data
                    })
                    that.onLoad();
                    setTimeout(() => {
                      wx.showToast({
                        title: 'كىردىڭىز',
                        icon: "success"
                      });
                    }, 100);
                  }
                }
              })
            } else {
              console.log('登录失败！' + res.errMsg)
            }
          }
        })
      }
    })

  },

  // 获取观看记录
  korganlirim:function(){
    var openid = wx.getStorageSync('userinfo').openid;
    wx.navigateTo({
      url: '/pages/korganlirim/kinolst?openid=' + openid,
    })
  },

  // 收藏
  Sahliwalginim:function(){
    var openid = wx.getStorageSync('userinfo').openid;
    wx.navigateTo({
      url: '/pages/sahliwalginim/kinolst?openid=' + openid,
    })
  },


  // 关于
  Heqqida:function(){
    wx.navigateTo({
      url: '/pages/heqqida/heqqida',
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 2
      })
    }

    //判断一下存储的用户名
    var userinfo = wx.getStorageSync('userinfo');
    this.setData({
      userinfo: userinfo
    })
    if (userinfo.openid == null || userinfo.openid == 'undefined') {
      this.setData({
        login: false
      })
    } else {
      this.setData({
        login: true
      })
    }
    this.setData({
      userinfo: userinfo
    })
  },

  outlogin: function () {
    wx.removeStorageSync('userinfo');
    this.onShow();
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})